﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RescueApp.Data;
using EFCoreRepository.Extensions;
using RescueApp.Models;
using Microsoft.Data.SqlClient;

namespace RescueApp.UI
{
    public partial class frmDispatchManagement : Form
    {
        private RescueContext db;

        public frmDispatchManagement()
        {
            InitializeComponent();
        }

        public frmDispatchManagement(RescueContext db)
        {
            InitializeComponent();
            this.db = db;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "SELECT * FROM [dbo].[RescueDispatch]";
            loadData(sql, dataGridView1);
        }

        private void loadData(string sql, DataGridView dg)
        {
            DataTable dt = db.SqlDataTable(sql, null);
            dg.DataSource = dt;
            dg.Refresh();
        }

        private void frmDispatchManagement_Load(object sender, EventArgs e)
        {
            string sql = "SELECT * FROM [dbo].[RescueDispatch]";
            loadData(sql, dataGridView1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            frmNewDispatch frm = new frmNewDispatch(db);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                frmDispatchManagement_Load(this, null);
            }
            frm.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int idx;
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择需要更新的行");
                return;
            }
            idx = dataGridView1.SelectedRows[0].Index;
            int Id = int.Parse(dataGridView1.Rows[idx].Cells["DispatchID"].Value.ToString());
            string sql = "SELECT * FROM [dbo].[RescueDispatch] WHERE [DispatchID] = @Id";
            IDictionary<string, object> dict = new Dictionary<string, object>();
            dict["@Id"] = Id;
            SqlParameter[] parameters = dict.ToSqlParameters();
            List<RescueDispatch> dispatches = db.SqlQuery<RescueDispatch>(sql, parameters).ToList();
            var dispatch = dispatches.FirstOrDefault();
            if (dispatch == null)
            {
                MessageBox.Show("未找到派遣记录");
                return;
            }
            frmUpdateDispatch frm = new frmUpdateDispatch(db, dispatch);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                frmDispatchManagement_Load(this, null);
            }
            frm.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int idx;
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择需要删除的行");
                return;
            }
            idx = dataGridView1.SelectedRows[0].Index;
            int Id = int.Parse(dataGridView1.Rows[idx].Cells["DispatchID"].Value.ToString());
            if (MessageBox.Show($"是否确定删除ID为 {Id} 的派遣记录？", "确认删除", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string sql = "DELETE FROM [dbo].[RescueDispatch] WHERE [DispatchID] = @Id";
                IDictionary<string, object> dict = new Dictionary<string, object>();
                dict["@Id"] = Id;
                SqlParameter[] parameters = dict.ToSqlParameters();
                try
                {
                    int affectedRows = db.ExecuteSql(sql, parameters);
                    if (affectedRows > 0)
                    {
                        MessageBox.Show("删除成功！");
                        frmDispatchManagement_Load(this, null);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}